<?php
 
 class Chanel_Model_Index extends Zend_Db_Table
 {
 	protected $_name="chanel";
    protected $_primary = 'id';
    
 	public function insertChanel($arrParam = null, $option = null)
  	{	
  		  		
  		$row = $this->fetchNew();		  	
		$row->name       = @$arrParam['name'];
		$row->userid           =@$arrParam['userid'];			
		$row->create_date       =@$arrParam['create_date'];
		$row->description       =@$arrParam['description'];
		$row->type       =@$arrParam['type'];
		$row->logo       =@$arrParam['logo'];
		$row->location       =@$arrParam['location'];
		$row->cover       =@$arrParam['cover'];
	  	return $row->save();
	  	
  	}
  	
  	public function listItemAll($arrParam = null, $option = null){
  		
  		$db = Zend_Registry::get('connectDB');
		$pagesize = $arrParam['pagesize'];	
  		$offset = ($arrParam['page'] - 1) * $pagesize;
  		
  		$sql = "select * from chanel limit ".$offset.",".$pagesize;
  		
  		$result = $db->fetchAll($sql);
		
  		return $result;
  		
  	}
  	
 	public function listItem($arrParam = null, $option = null){
		
 		$db = Zend_Registry::get('connectDB');
		$pagesize = $arrParam['pagesize'];	
  		$offset = ($arrParam['page'] - 1) * $pagesize;
  		
  		if($arrParam["userid"] != ""){
  			
  			$sql = "select * from chanel where id not  in  (select followed from  followchanel where follower = ".$arrParam["userid"].") and id <> ".$arrParam["userid"]." limit ".$offset.",".$pagesize;
  			
  		}
  		else{

  			$sql = "select * from chanel limit ".$offset.",".$pagesize;
  			
  		}
  		
  		$result = $db->fetchAll($sql);
		
  		return $result;
  	}
  	
  	
  	public function countItem($arrParam = null, $option = null){
		$db = Zend_Registry::get('connectDB');
		$pagesize = $arrParam['pagesize'];	
  		$offset = ($arrParam['page'] - 1) * $pagesize;
  		
  		if($arrParam["userid"] != ""){
  			
  			$sql = "select count(*) from chanel where id not  in  (select followed from  followchanel where follower = ".$arrParam["userid"].") and id <> ".$arrParam["userid"]." limit ".$offset.",".$pagesize;
  			
  		}
  		else{

  			$sql = "select count(*) as count from chanel limit ".$offset.",".$pagesize;
  			
  		}
  		
  		$count = $db->fetchOne($sql);
		
  		return $count;
  		
  	}
  	
  	public function findItem($val){
  		$db = Zend_Registry::get('connectDB');
  		$sql = "select * from chanel where type LIKE '%".$val."%'";
  		$result = $db->fetchAll($sql);
		
  		return $result;
  	}
  	
 	public function updateChanel($arrParam = null, $option = null)
  	{  		
  		
  		$db = Zend_Registry::get('connectDB');	
		$where = array(
    		'id = ?' => $arrParam['id'],
			'userid = ?' => $arrParam['userid'],		
		);	
  		$data = array();
			
		if (@$arrParam['name'] != ""){
			$data["name"]=$arrParam['name'];
		}
		
		if (@$arrParam['create_date'] != ""){
			$data["create_date"]=$arrParam['create_date'];
		}
		
		if (@$arrParam['description'] != ""){
			$data["description"]=$arrParam['description'];
		}
		
		if (@$arrParam['type'] != ""){
			$data["type"]=$arrParam['type'];
		}			
		if (@$arrParam['logo'] != ""){				
			$data["logo"]=$arrParam['logo'];				
		}			
		if (@$arrParam['location'] != ""){
			$data["location"]=$arrParam['location'];
		}
  		if (@$arrParam['cover'] != ""){
			$data["cover"]=$arrParam['cover'];
		}
		
		return $db->update("chanel", $data, $where); 		
  	  	
  	}
  	
 	public function getChanelById($id){
	
  		$db = Zend_Registry::get('connectDB');		
		$sql = "SELECT *  FROM chanel where id =  '".$id."'";
  		
		return $db->fetchRow($sql);
	  	
	  		
	}
  	
 }
 
 ?>